package org.lztvn.elearning.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;
import javax.persistence.Query;

import org.lztvn.elearning.entities.Content;
import org.lztvn.elearning.entities.Unit;

public class UnitDaoImpl implements IUnitDao{
	@PersistenceContext
	private EntityManager em;
	@Override
	public Long addUnit(Unit u) {
		 em.persist(u);
		 return u.getIdUnit();
	}

	@Override
	public void deleteUnit(Long idU) {
		Unit u = em.find(Unit.class, idU);
		em.remove(u);
		
	}

	@Override
	public void modifierUnit(Unit u) {
		em.merge(u);
		em.flush();  
		em.close();
	}

	@Override
	public Unit getUnit(Long idU) {
		return em.find(Unit.class, idU);
	}

	@Override
	public List<Unit> listUnitByCourseId() {
		//Query req = em.createQuery("select u from Unit u where u.course.idCourse=:x");
		Query req = em.createQuery("select u from Unit u order by u.pos ASC");
		//req.setParameter("x",courseId);
		return req.getResultList();
	}

	
	@Override
	public List<Unit> listUnitByCourseId_bis(Long idc) {
		Query req = em.createQuery("select u from Unit u where u.idCourse=:x");
		req.setParameter("x",idc);
		return req.getResultList();
	}
}
